

var _DB_ = require('./DB');
var uuid = require('uuid');
var DB = new _DB_();


var makeError = error => {
    return {
        type: 'error',
        message: error,
        res: null,
    }
}

module.exports = {
    service: {
        // 创建服务器信息
        creatService: async data => {
            var _id = uuid.v1();
            var _create_time = new Date().getTime();
            var sql = `INSERT INTO service (
                service_id, 
                title, 
                ip, 
                username, 
                password, 
                update_time, 
                create_time
            ) VALUES (
                 '${_id}',
                 '${data.title}',
                 '${data.ip}',
                 '${data.username}',
                 '${data.password}'
                 ,${_create_time},
                 ${_create_time}
            ) `;
            try {
                await DB.creat(sql);
                data.serviceId = _id;
                data.updateTime = _create_time;
                data.createTime = _create_time;
                return {
                    type: 'success',
                    res: data,
                };
            } catch (error) {
                return makeError(error);
            }
        },
        // 获取连接过得服务器信息
        getService: async id => {
            try {
                var data = await DB.get(`SELECT * FROM service WHERE service_id = '${id}'`);
                return {
                    type: 'success',
                    res: data
                };
            } catch (error) {
                return makeError(error);
            }
        },
        // 获取服务器列表
        getAllService: async () => {
            try {
                var data = await DB.find(`SELECT * FROM service`);
                return {
                    type: 'success',
                    res: data
                };
            } catch (error) {
                return makeError(error);
            };
        },
        // 更新服务器信息
        updateService: async (data) => {
            var _update_time = new Date().getTime();
            var sql = `UPDATE service SET 
                        title = '${data.title}',
                        ip = '${data.ip}', 
                        username = '${data.username}', 
                        password = '${data.password}', 
                        update_time = ${_update_time}
                        WHERE service_id = '${data.serviceId}';`;
            try {
                await DB.update(sql);
                data.updateTime = _update_time;
                return {
                    type: 'success',
                    res: data,
                };
            } catch (error) {
                return makeError(error);
            }
        }
    },
    user: {
        // 获取用户信息
        getMainUser: async () => {
            try {
                var data = await DB.get(`SELECT * FROM user WHERE user_id = 'Local'`);
                return {
                    type: 'success',
                    res: data
                };
            } catch (error) {
                return makeError(error);
            }
        },
    },
    project: {
        // 获取本地项目信息
        getProject: async (id) => {
            try {
                var data = await DB.get(`SELECT * FROM project WHERE project_id = '${id}'`);
                return {
                    type: 'success',
                    res: data
                };
            } catch (error) {
                return makeError(error);
            }
        },
    },
    theme: {
        // 获取皮肤信息
        getTheme: async () => {
            try {
                var data = await DB.get(`SELECT * FROM option WHERE key = 'theme'`);
                return {
                    type: 'success',
                    res: data
                };
            } catch (error) {
                return makeError(error);
            }
        },
        // 更新皮肤信息
        setTheme: async (data) => {
            var Sql = `UPDATE option SET value = '${data}' WHERE key = 'theme'`;
            try {
                await DB.update(Sql);
                return {
                    type: 'success',
                    res: data,
                };
            } catch (error) {
                return makeError(error);
            };
        }
    },
}